﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XGE.ComponentSystem;

namespace XGE
{
    public class Core : Microsoft.Xna.Framework.Game, ICoreGame 
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        SpriteFont Test;
        bool breakTest;
        int LineCount = 0;
        KeyboardState PreviousKeyboardState;
        List<string> Text = new List<string>();

        private ComponentCollection GameComponents;
        private Physics.PhysicsSystem physicsSystem;

        public Core()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            GameComponents = new ComponentCollection(this);
        }

        protected override void Initialize()
        {
            physicsSystem = new XGE.Physics.PhysicsSystem(this);
            base.Initialize();
        }

        public void LoadComponents(List<Object> Components)
        {
            foreach (IDrawableObject gameComponent in Components)
            {
                this.GameComponents.AddComponent(gameComponent);
            }
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            GameComponents.LoadContent();
            GameComponents.Initialize();
           // Test = Content.Load<SpriteFont>("TestFont");
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            //This method updates all of the gamecomponents
            base.Update(gameTime);
            UpdatePhysics(gameTime);
            GameComponents.Update(gameTime);
            //Other systems should be placed here to update after the gamecomponets;
        }

        protected void UpdatePhysics(GameTime gameTime)
        {
            physicsSystem.UpdatePhysics(ref GameComponents, gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            GameComponents.Draw(gameTime);
            base.Draw(gameTime);
            spriteBatch.End();
        }

        #region ICoreGame Members

        Microsoft.Xna.Framework.Content.ContentManager ICoreGame.Content
        {
            get { return Content; }
        }

        SpriteBatch ICoreGame.spriteBatch
        {
            get { return spriteBatch; }
        }
        bool ICoreGame.BreakTest
        {
            get
            {
                return breakTest;
            }
            set
            {
                breakTest = value;
            }
        }

        #endregion
    }

}
